package com.jchen.auth.config;

import com.jchen.common.constant.QueueConstant;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 消息监听
 * @author liyang
 * @date 2021/6/22 16:06
 */
@Configuration
public class QueueMsgListenerConfig {

    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer(){
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);

        container.setConcurrentConsumers(1);
        container.setMaxConcurrentConsumers(1);
        // RabbitMQ默认是自动确认，这里改为手动确认消息
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        /*
         * 设置一个队列
         * 如需同时设置多个可以使用setQueueNames方法
         * container.setQueueNames("Queue1","Queue2")
         */
        container.setQueueNames(QueueConstant.JCHEN_QUEUE_NAME,QueueConstant.SYS_QUEUE_NAME);
        //两种方式处理消息@RabbitListener及下面方式
        //container.setMessageListener(noticeQueueAckRecv);
        return container;
    }
}
